package com.sxt.bus.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sxt.bus.domain.Customer;
import com.sxt.bus.service.CustomerService;
import com.sxt.bus.utils.ExportCustomerUtils;
import com.sxt.bus.vo.CustomerVo;
import com.sxt.sys.utils.DataGridView;


@Controller
@RequestMapping("customer")
public class CustomerController {
	      @Autowired
       private CustomerService customerService;
		
		/**
		 * 跳转到客户管理的页面
		 */
		@RequestMapping("toCustomerManager")
		public String toCustomerManager(){
			return "business/customerManager";
		}
		/**
		 * 查询
		 */
		@RequestMapping("loadAllCustomer")
		@ResponseBody
		public DataGridView loadAllCustomer(CustomerVo customerVo){
			/*DataGridView dg = this.customerService.queryAllCustomer(customerVo);
			System.out.println(dg.getTotal());*/
			return  this.customerService.queryAllCustomer(customerVo);
		}
		@RequestMapping("addCustomer")
		@ResponseBody
		public Map<String,Object> addCustomer(CustomerVo customerVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.customerService.addCustomer(customerVo);
				 map.put("msg", "添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "添加失败");
			}
			return  map;
		}
		@RequestMapping("updateCustomer")
		@ResponseBody
		public Map<String,Object> updateCustomer(CustomerVo customerVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.customerService.updateCustomer(customerVo);
				 map.put("msg", "修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "修改失败");
			}
			return  map;
		}
		
		//删除
		/*@RequestMapping("deleteCustomer")
		@ResponseBody
		public Map<String,Object> deleteCustomer(CustomerVo customerVo){
			Map<String,Object> map=new HashMap<>();
			 try {
				 this.customerService.deleteCustomer(customerVo);
				 map.put("msg", "添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				 map.put("msg", "添加失败");
			}
			return  map;
		}*/
		//删除
		@RequestMapping("deleteCustomer")
		@ResponseBody
		public void deleteCustomer(CustomerVo customerVo){
			
				 this.customerService.deleteCustomer(customerVo);
		}
		
		/**
		 * 导出客户数据的方法
		 */
		@RequestMapping("exportCustoner")
		public void exportCustoner(CustomerVo customerVo,HttpServletResponse response){
			//查询客户数据
			List<Customer> customers=this.customerService.queryAllCustomerForList(customerVo);
			String fileName="客户数据.xls";
			ExportCustomerUtils.export(customers,fileName,response);
		}
		
		
		
		
	}
